<?php
/**
 * rocketMQ控制台
 * Created by PhpStorm.
 * User: zhangyajie
 * Date: 2020/11/11
 * Time: 09:18
 */

namespace App\Utils;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;


class RocketMQ{

    public $accessKeyId = 'LTAI4GFyQ2yRnTG4UKGXcUwq';
    public $accessSecret = 'bJ893KlddGsn7ZViGHTwqztjevubZn';
    public $regionId = 'cn-beijing';
    public $host = 'ons.cn-beijing.aliyuncs.com';
    public $instanceId = 'MQ_INST_50112006_BXYle0HK';

    public function __construct()
    {
        AlibabaCloud::accessKeyClient($this->accessKeyId, $this->accessSecret)
            ->regionId($this->regionId)
            ->asDefaultClient();
    }



    /**
     *  获取topic列表
     * @return array|string
     */
    public function onsTopicList()
    {
        $action = 'OnsTopicList';
        $queryData = [
            'query' => [
                'RegionId' => $this->regionId,
                'InstanceId' => $this->instanceId,
            ],
        ];

        return $this->onsService($action, $queryData);
    }


    /**
     * 创建topic
     * @param $topic  1.只能包含英文、数字、 短横线（-）和下划线（_）。 长度限制在3~64个字符之间。
     * @param int $messageType  0：普通消息,1：分区顺序消息,2：全局顺序消息,4：事务消息,5：定时/延时消息
     * @return array|string
     */
    public function onsTopicCreate($topic, $messageType = 0)
    {
        $action = 'OnsTopicCreate';
        $queryData = [
            'query' => [
                'RegionId' => $this->regionId,
                'InstanceId' => $this->instanceId,
                'MessageType' => $messageType,
                'Topic' => $topic,
            ],
        ];

        return $this->onsService($action, $queryData);
    }


    /**
     * 删除topic
     * @param $topic
     * @return array|string
     */
    public function onsTopicDelete($topic)
    {
        $action = 'OnsTopicDelete';
        $queryData = [
            'query' => [
                'RegionId' => $this->regionId,
                'InstanceId' => $this->instanceId,
                'Topic' => $topic,
            ],
        ];

        return $this->onsService($action, $queryData);
    }

    /**
     * 查询当前 Topic 下的消息总量以及 Topic 的最后更新时间
     * @param $topic
     * @return array|string
     */
    public function onsTopicStatus($topic)
    {
        $action = 'OnsTopicStatus';
        $queryData = [
            'query' => [
                'RegionId' => $this->regionId,
                'InstanceId' => $this->instanceId,
                'Topic' => $topic,
            ],
        ];

        return $this->onsService($action, $queryData);
    }


    /**
     * 创建客户端 Group ID
     *
     * @param $groupId  1.以 “GID_“ 或者 “GID-“ 开头，只能包含字母、数字、短横线（-）和下划线（_）, 长度限制在 5–64 字节之间。
     * @param string $groupType 指定创建的 Group ID 适用的协议。TCP 协议和 HTTP 协议的 Group ID 不可以共用，需要分别创建。取值说明如下：
     * tcp：默认值，表示创建的 Group ID 仅适用于 TCP 协议的消息收发。
     * http：表示创建的 Group ID 仅适用于 HTTP 协议的消息收发。
     * @param string $remark
     * @return array|string
     */
    public function onsGroupCreate($groupId, $groupType = 'http', $remark = '')
    {
        $action = 'OnsGroupCreate';
        $queryData = [
            'query' => [
                'InstanceId' => $this->instanceId,
                'GroupId' => $groupId,
                'GroupType' => $groupType,
                'Remark' => $remark,
            ],
        ];

        return $this->onsService($action, $queryData);
    }


    /**
     * 展示 Group ID 资源的列表
     *
     * @param $groupId  1.查询指定 Group ID 时设置，否则查询所有 Group ID。
     * @param string $groupType
     * @return array|string
     */
    public function onsGroupList($groupId = '', $groupType = 'http')
    {
        $action = 'OnsGroupList';
        $queryData = [
            'query' => [
                'InstanceId' => $this->instanceId,
                'GroupId' => $groupId,
                'GroupType' => $groupType,
            ],
        ];

        return $this->onsService($action, $queryData);
    }

    public function onsGroupSubDetail($groupId)
    {
        $action = 'OnsGroupSubDetail';
        $queryData = [
            'query' => [
                'InstanceId' => $this->instanceId,
                'GroupId' => $groupId,
            ],
        ];

        return $this->onsService($action, $queryData);
    }

    public function onsService($action, $queryData = []){
        try {
            $result = AlibabaCloud::rpc()
                ->product('Ons')
                // ->scheme('https') // https | http
                ->version('2019-02-14')
                ->action($action)
                ->method('POST')
                ->host($this->host)
                ->options($queryData)
                ->request();
            return $result->toArray();
        } catch (ClientException $e) {
            return $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            return $e->getErrorMessage() . PHP_EOL;
        }
    }



}










